package fr.generali.accueilclient.gwt.client.ui.components.tabheader;

import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.resources.client.ImageResource.ImageOptions;
import com.google.gwt.resources.client.ImageResource.RepeatStyle;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class ValidTabHeader extends Composite {

    private static ValidTabHeaderUiBinder uiBinder = GWT.create(ValidTabHeaderUiBinder.class);

    interface ValidTabHeaderUiBinder extends UiBinder<Widget, ValidTabHeader> {
    }

    @UiField
    protected Label tabLabel;

    @UiField
    protected FlowPanel validPanel;

    @UiField
    protected ValidTabHeaderResources res;

    public ValidTabHeader(String label) {
        initWidget(uiBinder.createAndBindUi(this));
        tabLabel.setText(label);
        res.style().ensureInjected();
        setValid(ValidState.NONE);
    }

    public void setValid(ValidState valid) {
        switch (valid) {
            case INVALID:
                validPanel.removeStyleName(res.style().valid());
                tabLabel.addStyleName(res.style().invalid());
                break;
            case NONE:
                validPanel.removeStyleName(res.style().valid());
                tabLabel.removeStyleName(res.style().invalid());
                break;
            case VALID:
                validPanel.addStyleName(res.style().valid());
                tabLabel.removeStyleName(res.style().invalid());
                break;
            default:
                break;
        }
    }

    public void setValid(boolean valid) {
        if (valid) {
            setValid(ValidState.VALID);
        } else {
            setValid(ValidState.INVALID);
        }
    }

    public static enum ValidState {
        VALID, INVALID, NONE
    }

    interface ValidTabHeaderResources extends ClientBundle {

        @Source("components/tabheader/image/picto-ok-cercle.png")
        @ImageOptions(repeatStyle = RepeatStyle.None)
        ImageResource validPicto();

        @Source("components/tabheader/tabheader.css")
        Style style();

        interface Style extends CssResource {

            String invalid();

            String valid();
        }
    }
}
